package com.filldream.fastboot.web.controller.pc;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.filldream.fastboot.base.entity.SysDept;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.pc.sysDept.AddDeptSTO;
import com.filldream.fastboot.base.request.pc.sysDept.ModifyDeptSTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.core.LoginHolder;
import com.filldream.fastboot.core.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * @author RickSun
 * @version 1.0
 * @data 2021-09-18 16:06
 * @description PcDatabaseController
 */
@AllArgsConstructor
@Api(tags = "PC-部门管理")
@RestController
@RequestMapping("/pc/dept")
public class PcSysDeptController {

    private final SysDeptService sysDeptService;

    @Log(value = "添加部门")
    @ApiOperation(value = "添加部门")
    @PostMapping("/add")
    @RolePermissions(value = "dept:add")
    public B add(@RequestBody @Valid AddDeptSTO addDeptSTO){
        return sysDeptService.saveDept(addDeptSTO);
    }

    @Log(value = "部门列表-树")
    @ApiOperation(value = "部门列表-树")
    @GetMapping("/tree")
    @RolePermissions(value = "dept:tree")
    public R<List<Tree<String>>> tree(){
        return sysDeptService.tree();
    }

    @Log(value = "部门列表")
    @ApiOperation(value = "部门列表")
    @GetMapping("/list")
    @RolePermissions(value = "dept:list")
    public R<List<SysDept>> list(Long parentId){
        parentId = parentId == null ? LoginHolder.getPcUser().getLinkId() : parentId;
        List<SysDept> list = sysDeptService.list(
                new QueryWrapper<SysDept>().eq("del_flag", 0).orderByAsc("dept_rank")
                        .eq("parent_id",parentId)
        );
        return R.successd(list);
    }

    @Log(value = "修改部门")
    @ApiOperation(value = "修改部门")
    @PostMapping("/modify")
    @RolePermissions(value = "dept:modify")
    public B modify(@RequestBody @Valid ModifyDeptSTO modifyDeptSTO){
        return sysDeptService.modifyDept(modifyDeptSTO);
    }

    @Log(value = "删除部门")
    @ApiOperation(value = "删除部门")
    @PostMapping("/del")
    @RolePermissions(value = "dept:del")
    public B del(@RequestBody @Valid IdSTO idSTO){
        return sysDeptService.del(idSTO.getId());
    }

}
